import java.util.ArrayList;
import java.util.List;

abstract class AbstractFile {
    protected String name;
    public void printName() {
        System.out.println(name);
    }
    public abstract boolean addChild(AbstractFile file);
    public abstract boolean removeChild(AbstractFile file);
    public abstract List<AbstractFile> getChild();
}
class File extends AbstractFile {
    public File(String name) {
        this.name = name;
    }
//    文件下是没有孩子的，所以这三个方法只是保证对象的使用具有一致性
    @Override
    public boolean addChild(AbstractFile file) {
        return false;
    }
    @Override
    public boolean removeChild(AbstractFile file) {
        return false;
    }
    @Override
    public List<AbstractFile> getChild() {
        return null;
    }
}
class Folder extends AbstractFile {
    private final List<AbstractFile> children;
    public Folder(String name) {
        this.name = name;
        this.children = new ArrayList<>();
    }
    @Override
    public boolean addChild(AbstractFile file) {
        return children.add(file);
    }
    @Override
    public boolean removeChild(AbstractFile file) {
        return children.remove(file);
    }
    @Override
    public List<AbstractFile> getChild() {
        return children;
    }
}
public class Test {
    public static void main(String[] args) {
//        构建一个目录树
        AbstractFile rootFolder=new Folder("C:\\");
        AbstractFile Windows=new Folder("Windows");
        AbstractFile System32=new Folder("System32");
        AbstractFile ProgramFiles=new Folder("Program Files");
        AbstractFile java=new Folder("java");
        AbstractFile jdk=new File("jdk");
        AbstractFile file=new File("我是一个文件");
        rootFolder.addChild(Windows);
        rootFolder.addChild(ProgramFiles);
        rootFolder.addChild(file);
        Windows.addChild(System32);
        ProgramFiles.addChild(java);
        ProgramFiles.addChild(jdk);

//         打印目录树
        printFileTree(rootFolder);
    }
    public static void printFileTree(AbstractFile file) {
        file.printName();
        List<AbstractFile> children = file.getChild();
        if (children==null)return;
        for (AbstractFile child : children) {
            printFileTree(child);
        }
    }
}
